//
//  AudioPlayer.m
//  Share
//
//  Created by Lin Zhang on 11-4-26.
//  Copyright 2011年 www.eoemobile.com. All rights reserved.
//

#import "AudioPlayer.h"
#import "AudioButton.h"
#import "JingRoundView.h"
#import "AVPlayer+player.h"
#import "UIImageView+WebCache.h"
@implementation AudioPlayer

@synthesize myPlayer, button, url;


+ (AudioPlayer *)shareInstance
{
    static AudioPlayer *audioplayer = nil;
    
    if (audioplayer == nil) {
        audioplayer = [[AudioPlayer alloc] init];
        
    }
    
    return audioplayer;
}

- (id)init
{
    
    self = [super init];
    if (self) {
           self.playmodel = @"One";
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
    [url release];
    [myPlayer release];
    [button release];
    [_timer invalidate];
}


- (BOOL)isProcessing
{
    return self.myPlayer.status;
}


- (void)setImagePath:(NSString *)imagePath
{
    
    [self.roundView.roundImageView setImageWithURL:[NSURL URLWithString:[imagePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    
    
}

- (void)play
{        
    if (!myPlayer) {
        
        self.myPlayer = [AVPlayer shareAVPlayer];
        [self.myPlayer playWithURL:self.url isPlay:1];
        [self setupTimer];
        self.isPlay = YES;
         [button startSpin];
           }
    
    
    if (self.isPlay == YES) {
        
        [self.myPlayer play];
         button.image = [UIImage imageNamed:stopImage];
        [button setColourR:0.1 G:1.0 B:0.1 A:1.0];
        
        self.roundView.isPlay = YES;
        [self.roundView startRotation];
        
        self.isPlay = NO;


    } else {
         [button setColourR:0.0 G:0.0 B:0.0 A:0.0];
               button.image = [UIImage imageNamed:playImage];

        [self.myPlayer pause];
        self.roundView.isPlay = NO;
        [self.roundView pauseRotation];
        self.isPlay = YES;

    }
    
    
    
}


- (void)stop
{    
    [button setProgress:0];
    [button stopSpin];

    button.image = [UIImage imageNamed:playImage];
    button = nil; // 避免播放器的闪烁问题        
    [button release];     
    

	if (myPlayer)
	{        
		[myPlayer pause];

		myPlayer = nil;
        
	}
}

- (void)updateProgress
{
    if (CMTimeGetSeconds(myPlayer.currentItem.currentTime) > 0.1) {
        [self.button stopSpin];
        
    }

    if (self.isPlay == NO) {
        [button setColourR:0.1 G:1.0 B:0.1 A:1.0];
    }
    if (CMTimeGetSeconds(myPlayer.currentItem.currentTime) < CMTimeGetSeconds(myPlayer.currentItem.duration)) {
        [button setProgress:CMTimeGetSeconds(myPlayer.currentItem.currentTime)/CMTimeGetSeconds(myPlayer.currentItem.duration)];
    } else {
        [button setProgress:0.0f];
        //[button setColourR:0.0 G:0.0 B:0.0 A:0.0];
       // button.image = [UIImage imageNamed:stopImage];
        
    }
    
    if (CMTimeGetSeconds(myPlayer.currentItem.currentTime) == CMTimeGetSeconds(myPlayer.currentItem.duration)) {
        
        if ([self.playmodel isEqualToString:@"One"]) {
            
            [self stop];
            
            [self.myPlayer playWithURL:self.url isPlay:1];
            
            [self play];
            
            
        }
        
        
    }
    
}


-(void) setupTimer
{
  
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
    

}



/*
 *  observe the notification listener when loading an audio
 */
- (void)playbackStateChanged
{
    if (self.isPlay == YES) {
        
        //[button stopSpin];
    } else {
        button.image = [UIImage imageNamed:playImage];
        //[button stopSpin];
        [button setColourR:0.0 G:0.0 B:0.0 A:0.0];
    }
    
    

    
    [button setNeedsLayout];    
    [button setNeedsDisplay];
}


@end
